package com.lovetemperature.action;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

import com.lovetemperature.model.Shop;
import com.lovetemperature.model.Staff;
import com.lovetemperature.service.ShopService;
import com.lovetemperature.service.StaffService;
import com.opensymphony.xwork2.ActionSupport;

public class OneShopAction extends ActionSupport {
	private ShopService shopService;
	private StaffService staffService;
	private List<Staff> candidateManager;
	private Shop shop;
	private String shopId;
	private String shopManagerId;
	private boolean newAdd;	//是否为新增店铺
	
	public String edit(){
		if(shopId!=null && !shopId.equalsIgnoreCase("")){	//已有店铺
			shop = shopService.findById(new Long(shopId));
			candidateManager = shop.getStaffList();
			newAdd = false;
		}else {	//新增店铺
			shop = new Shop();
			newAdd = true;
		}
		shopId = null;
		return "success";
	}
	
	public String submit(){
		if(shopId!=null && !shopId.equalsIgnoreCase("")){	//已有店铺
			if(!shopManagerId.equals("0")){
				Staff tmp = staffService.findById(Long.parseLong(shopManagerId));
				shop.setManager(tmp);
			}else{
				shop.setManager(null);
			}
			shopService.update(shop);
		}else {	//新增店铺
			shop.setManager(null);	//因为struts会自动给shop增加一个属性全为null的Staff对象,所以先将其去掉
			shopService.addShop(shop);
		}
		shopId = null;
		return "success";
	}
	
	public String delect(){
		if(shopId!=null && !shopId.equalsIgnoreCase("")){
			shopService.delect(Long.parseLong(shopId));
		}
		return "success"; 
	}

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public List<Staff> getCandidateManager() {
		return candidateManager;
	}

	public void setCandidateManager(List<Staff> candidateManager) {
		this.candidateManager = candidateManager;
	}

	public String getShopManagerId() {
		return shopManagerId;
	}

	public void setShopManagerId(String shopManagerId) {
		this.shopManagerId = shopManagerId;
	}

	public boolean isNewAdd() {
		return newAdd;
	}

	public void setNewAdd(boolean newAdd) {
		this.newAdd = newAdd;
	}

	public StaffService getStaffService() {
		return staffService;
	}

	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}

}
